﻿using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using CommServer.Common;
using System;
using CommServer.Common.Database;
using CommServer.Tasks;
using CommServer.Tasks.Common;
using CommServer.Cache;
using System.Collections.Generic;
using CommServer.Upgrade;
using CommServer.Network;
using System.Threading;

namespace CommServer
{
    public class Program
    {
        public static Dictionary<string, upgradeInv> upgradeInvs = new Dictionary<string, upgradeInv>();
        public static Dictionary<string, upgradeDevice> upgradeDevs = new Dictionary<string, upgradeDevice>();
        public static IWebHost host;
        private static AppManager app;
        public static void Main(string[] args)
        {
            string HttpPort = "10000";
            int DevicePort = 9999;
            try
            {
                HttpPort = Config.GetIntValue("HttpPort").ToString();
                DevicePort = Config.GetIntValue("DevicePort");
            }
            catch (Exception ex)
            {
                Log.Error("read conig file error:" + ex.Message);
            }
            try
            {
                app = AppManager.Load(DevicePort,Convert.ToInt16(HttpPort));

                while(true)
                {
                    if (app.Running)
                    {
                        Thread.Sleep(10);
                        continue;
                    }
                    app.Start();
                } 


            }
            catch (Exception ex)
            {
                Console.WriteLine("application runtime error " + ex.Message + "|" + ex.StackTrace)
;            }
        }

//        DbHelper.CreateTable();

//            CacheDevices.Load();
//            //启动定时存储任务(每个5秒批量插入)
//            m_Task_DataSave.Start();



//            StartDeviceServer(DevicePort);




//Console.WriteLine("device port:" + DevicePort);
//            StartHttpServer(HttpPort);
        public static DeviceServer GetDeviceServer()
        {
            return app.GetDeviceServer();
        }
        public static void restart()
        {
            app.Restart();
        }
 
 

    }
}
